<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

/**
 * App\RiskRuleAli
 *
 * @property int $id
 * @property string|null $zhfx 综合风险
 * @property string|null $gzfx 共租风险
 * @property string|null $yqfx 逾期风险
 * @property int|null $status 状态
 * @property string|null $level 等级
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $money 金额限制
 * @property string|null $age 年龄限制
 * @property string|null $ultimately 最终综合条件
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereAge($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereGzfx($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereUltimately($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereYqfx($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\RiskRuleAli whereZhfx($value)
 * @mixin \Eloquent
 */
class RiskRuleAli extends Model
{
    protected $table = 'risk_rule_ali';

    const MZ_MAP = [
        ['id'=>1, 'rank'=>'RANK1', 'name'=>'极低','risk_name'=>'极低风险'],
        ['id'=>2, 'rank'=>'RANK2', 'name'=>'低','risk_name'=>'低风险'],
        ['id'=>3, 'rank'=>'RANK3', 'name'=>'中','risk_name'=>'中风险'],
        ['id'=>4, 'rank'=>'RANK4', 'name'=>'高','risk_name'=>'高风险'],
        ['id'=>5, 'rank'=>'RANK5', 'name'=>'极高','risk_name'=>'极高风险'],
    ];

    const LEVEL_MAP = [
        'S' => 'S',
        'A1' => 'A1',
        'A2' => 'A2',
        'B' => 'B',
        'C1' => 'C1',
        'C2' => 'C2',
        'D' => 'D',
    ];

    //获取命中率和命中规则
    public static function getZhanbi($rule_ali_id)
    {
        $redis_key = 'rule_list_ali_zhanbi'.$rule_ali_id;
        $data = Redis::get($redis_key);
        if($data){
            $data = json_decode($data,true);
        }else{
            $count = RiskOrder::where('rule_ali_id',$rule_ali_id)->count();
            $all_count = RiskOrder::where('rule_ali_id','>',0)->count();
            if($all_count){
                $ratio = bcdiv($count,$all_count,4) * 100;
                $data = [
                    'count' => $count,
                    'all_count' => $all_count,
                    'ratio' => $ratio
                ];
            }else{
                $data = [
                    'count' => 0,
                    'all_count' => 0,
                    'ratio' => 0
                ];
            }
            Redis::set($redis_key,json_encode($data),'ex',300);
        }


        return $data;
    }

    public static function getRuleName()
    {
        $arr = [];
        foreach (self::MZ_MAP as  $value){
            $arr[$value['id']] = $value['name'];
        }
        return $arr;
    }

    public static function getRiskNameId()
    {
        $arr = [];
        foreach (self::MZ_MAP as  $value){
            $arr[$value['risk_name']] = $value['id'];
        }
        return $arr;
    }

    public static function getRiskRank(){
        $arr = [];
        foreach (self::MZ_MAP as  $value){
            $arr[$value['id']] = $value['rank'];
        }
        return $arr;
    }

    public static function getRuleList()
    {
        $redis_key = 'rule_list_ali';
        $rule = Redis::get($redis_key);
        if($rule){
            $rule = json_decode($rule,true);
        }else{
            $rule = self::where('status',0)->get();
            Redis::set($redis_key,json_encode($rule));
        }
        return $rule;
    }
}
